<?php
/**
 * 
 * comfiger
 * 2016-01-21
 **/
namespace Mama\Model;
use Think\Model;
class EventtypeModel extends Model {
	//定义表名称
	protected $tableName = 'eventtype';
	
	/**
	 * 获取收费类型列表
	 **/
	public function getEventTypeList($where=array()){
		//return $this->where($where)->order('typeid asc')->select();
		return $this->alias('event')
					->join('left join mama_event_attr as attr on event.typeid = attr.event_id')
					->field('event.*, attr.remark')
					->where($where)
					->order('event.typeid desc, attr.attr_id desc')
					->select();
	}
	
	/**
	 * 获取活动总条数
	 **/
	public function getEventTypeListCount($where){
		return $this->where($where)->count();
	}

	/**
	 * 根据条件查询活动
	 **/
	public function getEventInfoByWhere($where){
		return $this->where($where)->order('typeid asc')->select();
	}

	/**
	 * 获取收费类型详情
	 **/
	public function getCostTypeInfoByTypeId($typeid){
		return $this->where(array(
			'typeid' => $typeid
		))->find();
	}
	
	/**
	 * 编辑收费类型详情
	 **/
	public function editEventTypeInfoByTypeId($typeid, $setField){
		return $this->where(array(
			'typeid' => $typeid
		))->setField($setField);
	}
	
	/**
	 * 添加收费类型信息
	 **/
	public function addEventTypeInfo($data){
		return $this->data($data)->add();
	}
	
	public function deleteEventInfoById($typeid){
		return $this->where(array('typeid' => $typeid))->delete();
	}

	/**
	 * 添加规则
	 **/
	public function addEventTypeAttr($data){
		return M('event_attr')->addAll($data);
	}

	public function deleteEventTypeAttrByEventId($eventId){
		return M('event_attr')->where(array('event_id' => $eventId))->delete();
	}

	public function getEventTypeAttrByEventId($eventId){
		return M('event_attr')->where(array('event_id' => $eventId))->order('attr_month asc')->select();
	}

	/**
	 * 获取会员享受的活动
	 *
	 **/
	public function getMemberEventTypeByBtime($btime, $etime, $roomtype='All', $contract=''){
		$eventlist = array();
		if(!empty($contract)){
			$eventlist = $this->alias('events')
						->join('left join mama_event_attr as attr on events.typeid = attr.event_id')
						->field('events.*, attr.*')
						->where(array(
							'events.contract' => array('like', '%'. $contract . '%'),
							'events.type_status' => 1
						))->order('attr.attr_month desc,attr.rent_count desc, attr.property_count desc')
						->select();
		}
		//echo $this->_sql();
		//开始获取活动信息，type_btime <= 签约时间 <= type_etime
		if(!count($eventlist)){
			if($roomtype == 'All'){
				$where = array(
					'events.type_btime' => array('elt', strtotime($btime.' 00:00:00')),
					'events.type_etime' => array('egt', strtotime($btime.' 23:59:59')),
					'events.type_status' => 1,
					'events.contract' => ''
				);
			}else{
				$where = array(
					'events.type_btime' => array('elt', strtotime($btime.' 00:00:00')),
					'events.type_etime' => array('egt', strtotime($btime.' 23:59:59')),
					'events.type_status' => 1,
					'attr.room_type' => $roomtype //根据房型查询
					,'events.contract' => ''
				);
			}
			
			$eventlist = $this->alias('events')
							->join('left join mama_event_attr as attr on events.typeid = attr.event_id')
							->field('events.*, attr.*')
							->where($where)
							->order('attr.attr_month desc,attr.rent_count desc, attr.property_count desc')
							->select();
		}
		//echo $this->_sql();
		//临时数组
		$attrList = array();
		//判断是否有活动
		if(is_array($eventlist) && count($eventlist)){
			//计算合同签约月数
			//合同结束时间需要新增一天
			$netime = strtotime($etime);
			$etime = date('Y-m-d', strtotime("+1 day", $netime));
			//租期
			$month = getMonthNum($btime, $etime);
			
			//开始遍历规则
			foreach($eventlist as $ls){
				//if(strpos($ls['contract'], $contract) == false){
					//判断是否存在规则
					$flag = false;
					//判断活动的符号
					$symbol = $ls['symbol'];
					switch($symbol){
						case 0:
							if($month < $ls['attr_month']){
								if($attrList['attr_month'] < $ls['attr_month']){
									$attrList = $ls;
								}
							}
						break;
						case 1:
							if($month >= $ls['attr_month']){ //满足条件
								if($attrList['attr_month'] < $ls['attr_month']){
									$attrList = $ls;
								}
							}
						break;
						case 2:
							if($month > $ls['attr_month']){ // 大于活动月，满足条件
								if($attrList['attr_month'] < $ls['attr_month']){
									$attrList = $ls;
								}
							}
						break;
					}
					/*
					if($flag){
						$attrList = $ls;
						//跳出循环
						break;
					}*/
				//}
			}
		}
		return $attrList;
	}
}

